package com.hbzd.protection.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.sword.wechat4j.WechatSupport;
import org.sword.wechat4j.event.EventType;
import org.sword.wechat4j.event.MsgType;



/**
 * 
 * @author yang
 *
 * 2015年12月30日
 *
 */
public class MyWechat extends WechatSupport {
	
	
	private static Logger logger = Logger.getLogger(MyWechat.class);
	ApplicationContext context = null;
	
	public MyWechat(HttpServletRequest request) {
		super(request);
		context = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
	}

	public String getType(){
		String result = "欢迎关注中斗九州卫星定位微信平台，如果您是北斗会员，要查看您的车辆信息， <a href=\""+InitData.LOGIN+"\">请点击这里！</a>";
		execute();
		if(wechatRequest.getMsgType().equals(MsgType.event.name()) && wechatRequest.getEvent().equals(EventType.subscribe.name())){
			return result;
		}else{
			return "";
		}
	}
	/**
	 * 文本消息
	 */
	@Override
	protected void onText() {
		responseCustomerService();
	}
	/**
	 * 图片消息
	 */
	@Override
	protected void onImage() {
		responseCustomerService();
	}

	/**
	 * 语音消息
	 */
	@Override
	protected void onVoice() {
		responseCustomerService();
	}

	/**
	 * 视频消息
	 */
	@Override
	protected void onVideo() {
		responseCustomerService();
	}
	
	/**
	 * 地理位置消息
	 */
	@Override
	protected void onLocation() {
		String Location_X = wechatRequest.getLocation_X();
		String Location_Y = wechatRequest.getLocation_Y();
		String Scale = wechatRequest.getScale();
		String Label = wechatRequest.getLabel();
		String MsgId = wechatRequest.getMsgId();
		
		String result = "地理位置消息Location_X:" + Location_X + ", Location_Y:" + Location_Y + 
				", Scale:" + Scale + ", Label:" + Label + 
				", MsgId:" + MsgId;
		logger.info(result);
//		responseText(result);	
	}
	/**
	 * 链接消息
	 */
	@Override
	protected void onLink() {
		String Title = wechatRequest.getTitle();
		String Description = wechatRequest.getDescription();
		String Url = wechatRequest.getUrl();
		String MsgId = wechatRequest.getMsgId();
		
		String result = "链接消息Title:" + Title + ", Description:" + Description + 
				", Url:" + Url + 
				", MsgId:" + MsgId;
		logger.info(result);
		responseText(result);	
	}
	
	
	/**
	 * 未知消息类型，错误处理
	 */
	@Override
	protected void onUnknown() {
		responseCustomerService();
	}

	/**
	 * 扫描二维码事件
	 */
	@Override
	protected void scan() {
		String FromUserName = wechatRequest.getFromUserName();
		String Ticket = wechatRequest.getTicket();
		
		String result = "扫描二维码事件FromUserName:" + FromUserName + ", Ticket:" + Ticket;
		logger.info(result);
		responseText(result);
	}

	/**
	 * 订阅事件
	 * 当用户订阅时推送绑定事件
	 */
	@Override
	protected void subscribe() {
		String result = "欢迎关注中斗九州卫星定位微信平台，如果您是北斗会员，要查看您的车辆信息， <a href=\""+InitData.LOGIN+"\">请点击这里！</a>";
		logger.info(result);
		try {
			responseText(new String(result.getBytes("gbk")));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 取消订阅事件
	 * 判断该用户是否在员工表中，如果存在，将delflag置为1,openId置为空
	 */
	@Override
	protected void unSubscribe() {
		String FromUserName = wechatRequest.getFromUserName();
		String result = "取消订阅事件FromUserName:" + FromUserName;
		logger.info(result);
		
	}
	
	/**
	 * 点击菜单跳转链接时的事件推送
	 */
	@Override
	protected void view() {
		String link = super.wechatRequest.getEventKey();
		logger.info("点击菜单跳转链接时的事件推送link:" + link);
		responseText("点击菜单跳转链接时的事件推送link:" + link);
	}

	/**
	 * 自定义菜单事件
	 */
	@Override
	protected void click() {
		String result = "您还没有任务！";
		String key = super.wechatRequest.getEventKey();
		logger.info("自定义菜单事件eventKey:" + key);
		if(KeyType.JOB_LIST.name().equals(key)){
			//获取该员工的所有任务
		/*	jobServiceImpl = context.getBean(JobServiceImpl.class);
			result = jobServiceImpl.getList(super.wechatRequest.getFromUserName());*/
		}
		responseText(result);
	}
	
	/**
	 * 上报地理位置事件
	 */
	@Override
	protected void location() {
		String Latitude = wechatRequest.getLatitude();
		String Longitude = wechatRequest.getLongitude();
		String Precision = wechatRequest.getPrecision();
		String result = "上报地理位置事件Latitude:" + Latitude + ", Longitude:" + Longitude + ", Precision:" + Precision;
		logger.info(result);
		//responseText(result);
	}
	
	/**
	 * 模板消息发送成功推送事件
	 */
	@Override
	protected void templateMsgCallback() {
		String MsgID = wechatRequest.getMsgId();
		String Status = wechatRequest.getStatus();
		String result = "模板消息发送成功推送事件MsgID:" + MsgID + ", Status:" + Status;
		logger.info(result);
	}
	/**
	 * 弹出地理位置选择器的事件
	 */
	@Override
	protected void locationSelect() {
		String Location_X = wechatRequest.getSendLocationInfo().getLocation_X();
		String Location_Y = wechatRequest.getSendLocationInfo().getLocation_Y();
		String Scale = wechatRequest.getSendLocationInfo().getScale();
		String Label = wechatRequest.getSendLocationInfo().getLabel();
		String Poiname = wechatRequest.getSendLocationInfo().getPoiname();
		String result = "弹出地理位置选择器的事件Location_X:" + Location_X + 
				", Location_Y:" + Location_Y+ 
				", Scale:" + Scale+ 
				", Label:" + Label+ 
				", Poiname:" + Poiname;
		logger.info(result);
		responseText(result);
	}
	/**
	 * 弹出拍照或者相册发图的事件
	 */
	@Override
	protected void picPhotoOrAlbum() {
		String Count = wechatRequest.getSendPicsInfo().getCount();
		String PicMd5Sum = "";
		if(StringUtils.isNotBlank(Count) && !Count.equals("0")){
			PicMd5Sum = wechatRequest.getSendPicsInfo().getItem().get(0).getPicMd5Sum();
		}
		String result = "弹出系统拍照发图的事件Count:" + Count + ", PicMd5Sum:" + PicMd5Sum;
		logger.info(result);
		responseText(result);
	}
	/**
	 * 弹出系统拍照发图的事件
	 */
	@Override
	protected void picSysPhoto() {
		String Count = wechatRequest.getSendPicsInfo().getCount();
		String result = "弹出系统拍照发图的事件Count:" + Count ;
		logger.info(result);
		responseText(result);
	}
	/**
	 * 弹出微信相册发图器的事件推送
	 */
	@Override
	protected void picWeixin() {
		String Count = wechatRequest.getSendPicsInfo().getCount();
		String result = "弹出系统拍照发图的事件Count:" + Count ;
		logger.info(result);
		responseText(result);
	}
	/**
	 * 扫码推事件
	 * 
	 */
	@Override
	protected void scanCodePush() {
		String ScanType = wechatRequest.getScanCodeInfo().getScanType();
		String ScanResult = wechatRequest.getScanCodeInfo().getScanResult();
		String result = "扫码推事件ScanType:" + ScanType + ", ScanResult:" + ScanResult;
		logger.info(result);
		responseText(result);
	}
	/**
	 * 扫码推事件且弹出“消息接收中”提示框的事件
	 */
	@Override
	protected void scanCodeWaitMsg() {
		String ScanType = wechatRequest.getScanCodeInfo().getScanType();
		String ScanResult = wechatRequest.getScanCodeInfo().getScanResult();
		String result = "扫码推事件ScanType:" + ScanType + ", ScanResult:" + ScanResult;
		logger.info(result);
		responseText(result);
	}

	@Override
	protected void kfCloseSession() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void kfCreateSession() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void kfSwitchSession() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onShortVideo() {
		// TODO Auto-generated method stub
		
	}
	



}
